package com.lowcode.vo;

import com.lowcode.constant.CommonConstant;
import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private boolean success = true;

    private T data;

    private String errorCode = "";

    private String errorMessage = "";

    private Integer showType = 0;

    public Result() {}

    public Result(String code, String message, Integer showType) {
        this.errorCode = code;
        this.errorMessage = message;
        this.showType = showType;
    }

    public Result<T> success(String message) {
        this.errorMessage = message;
        this.errorCode = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }

    public static<T> Result<T> ok() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setErrorCode(CommonConstant.SC_OK_200);
        return r;
    }

    public static<T> Result<T> ok(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setErrorCode(CommonConstant.SC_OK_200);
        r.setData((T) msg);
        r.setErrorMessage(msg);
        return r;
    }

    public static<T> Result<T> ok(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setErrorCode(CommonConstant.SC_OK_200);
        r.setData(data);
        return r;
    }

    public static<T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setErrorCode(CommonConstant.SC_OK_200);
        return r;
    }

    public static<T> Result<T> OK(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setErrorCode(CommonConstant.SC_OK_200);
        r.setErrorMessage(msg);
        //Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
        r.setData((T) msg);
        return r;
    }

    public static<T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setErrorCode(CommonConstant.SC_OK_200);
        r.setData(data);
        return r;
    }

    public static<T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setErrorCode(CommonConstant.SC_OK_200);
        r.setErrorMessage(msg);
        r.setData(data);
        return r;
    }

    public static<T> Result<T> error(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setErrorCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        r.setErrorMessage(msg);
        r.setData(data);
        return r;
    }

    public static<T> Result<T> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    public static<T> Result<T> error(String code, String msg) {
        Result<T> r = new Result<T>();
        r.setErrorCode(code);
        r.setErrorMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public Result<T> error500(String message) {
        this.errorMessage = message;
        this.errorCode = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }

    /**
     * 无权限访问返回结果
     */
    public static<T> Result<T> noauth(String msg) {
        return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
    }

}
